﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

namespace SimpleSocxs.manage
{
    public partial class pages : AuthorizedPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
                ddlTemplates.DataSource = Helper.GetFileNamesInUserAndShared(SocxsUser.Name, "templates/*.master").Select(_=>Path.GetFileNameWithoutExtension(_));
        }

        protected void OnDeletePage(object sender, EventArgs args)
        {
            var pageID = int.Parse(((LinkButton)sender).CommandArgument);
            SocxsDataStore.Current.DeletePage(pageID);
        }

        protected void OnAddPage(object sender, EventArgs args)
        {
            var page_name = txtNewPageName.Text;
            var user = SocxsUser;
            var template_name = ddlTemplates.SelectedValue;
            SocxsDataStore.Current.AddPageFromTemplate(user.ID, page_name, template_name);
        }

        protected void Page_PreRender(object sender, EventArgs args)
        {
            rpt.DataSource = SocxsDataStore.Current.GetAllUserPages(SocxsUser.ID);
            DataBind();
        }
    }
}